package org.kia.activity.adapters;

import java.util.ArrayList;
import java.util.List;

import org.kia.R;
import org.kia.activity.SearchActivity;
import org.kia.data.model.CategoryModel;
import org.kia.widgets.SimpleWebImageView;

import android.content.Context;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

/**
 * List adapter for {@link SearchActivity}
 * 
 * @author Amir Lazarovich
 */
public class SearchListAdapter<T extends CategoryModel> extends BaseAdapter implements ListAdapter {

	// ////////////////////////////////////////////
	// Constants
	// ////////////////////////////////////////////

	private static final String TAG = "SearchListAdapter";

	// ////////////////////////////////////////////
	// Members
	// ////////////////////////////////////////////

	protected List<T> mData;
	protected LayoutInflater mInflater;

	// ////////////////////////////////////////////
	// Constructors
	// ////////////////////////////////////////////

	/**
	 * Constructs a new Search List Adapter
	 * 
	 * @param context
	 */
	public SearchListAdapter(Context context) {
		mInflater = LayoutInflater.from(context);
		mData = new ArrayList();
	}

	// ////////////////////////////////////////////
	// Public
	// ////////////////////////////////////////////

	/**
	 * Set new data
	 */
	public void setData(List<T> data) {
		mData = data;
		notifyDataSetChanged();
	}

	/**
	 * Get data
	 * 
	 * @return
	 */
	public List<T> getData() {
		return mData;
	}

	// ////////////////////////////////////////////
	// Overrides & Implementations
	// ////////////////////////////////////////////

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public T getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View converted, ViewGroup parent) {
		ViewHolder holder;

		if (converted == null) {
			converted = mInflater.inflate(R.layout.search_list_item, parent, false);
			holder = new ViewHolder();

			holder.image = (SimpleWebImageView) converted.findViewById(R.id.search_list_image);
			holder.image.setDefaultImageById(R.drawable.no_image);
			holder.name = (TextView) converted.findViewById(R.id.search_list_name);

			converted.setTag(holder);
		} else {
			holder = (ViewHolder) converted.getTag();
		}

		// Update our model view
		CategoryModel model = mData.get(position);
		holder.name.setText(model.getName());

		String oldImageUrl = holder.image.getLastImageUrl();
		if (model.getImage().equals(oldImageUrl)) {
			// Convert view didn't change since last time
			// thus there is no need to reset and load its image again
			return converted;
		}

		// calling reset is important to prevent old images from displaying in a recycled view.
		holder.image.reset();
		holder.image.setImageUrl(model.getImage());

		return converted;
	}

	// ////////////////////////////////////////////
	// Inner classes
	// ////////////////////////////////////////////

	/**
	 * ViewHolder pattern (used to decrease calls to findViewById method)
	 */
	static class ViewHolder {
		SimpleWebImageView image;
		TextView name;
	}

}
